﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;



namespace ScriptEase.Shell.Controls
{
    /// <summary>
    /// </summary>
    public partial class SplashScreen : Form
    {
        public SplashScreen()
        {
            InitializeComponent();
        }

        private static readonly EventWaitHandle _waitForm = new EventWaitHandle(false, EventResetMode.AutoReset);




        /// <summary>
        /// Open splash screen in a new thread.
        /// </summary>
        public void Start()
        {
            this.Show();
            ThreadPool.QueueUserWorkItem(x =>
            {
                while (!_waitForm.WaitOne(50))
                    Application.DoEvents();
            });
        }


        /// <summary>
        /// Stop thread that displays the splash screen and 
        /// close the splash screen.
        /// </summary>
        public void Stop()
        {
            _waitForm.Set();
            this.Close();
        }



        #region GIF Image Draw

        private void WaitLoad(object sender, EventArgs e)
        {
            if (ImageAnimator.CanAnimate(this.BackgroundImage))
                ImageAnimator.Animate(this.BackgroundImage, this.OnNextFrame);
            Application.DoEvents();
        }

        private void OnNextFrame(object o, EventArgs e)
        {
            this.Invalidate();
            //this.textBox1.Text = MessageText;
            Application.DoEvents();
        }

        /// <summary>
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            ImageAnimator.UpdateFrames();
            Application.DoEvents();
        }

        #endregion

    }
}